package com.hzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzj.mapper.VoteItemMapper;
import com.hzj.pojo.VoteOption;
import com.hzj.mapper.VoteOptionMapper;
import com.hzj.service.VoteOptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzj
 * @since 2021-03-19
 */
@Service
public class VoteOptionServiceImpl extends ServiceImpl<VoteOptionMapper, VoteOption> implements VoteOptionService {
    @Autowired
    VoteOptionMapper vom;
    @Autowired
    VoteItemMapper vim;

    @Override
    public List<Map<String, String>> checkNumTicket(int vsid) {

        return vom.checkNumTicket(vsid);
    }

    @Override
    public boolean delectOptions(String[] options, int vsid) {
        //删除投票选项的投票
        QueryWrapper wrapper = new QueryWrapper();
        if (options != null) {
            wrapper.notIn("vo_option", Arrays.asList(options));
        }
        wrapper.eq("vs_id", vsid);
        //查询出被删除的那个选项
        List<VoteOption> selectList = vom.selectList(wrapper);
        System.out.println("查询的删除选项列=====================》" + selectList);
        for (int i = 0; i < selectList.size(); i++) {
            VoteOption voteOption = selectList.get(i);
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("vo_id", voteOption.getVoId());
            int i1 = vim.delete(wrapper1);
        }
        //删除投票选项
        for (int i = 0; i < selectList.size(); i++) {
            VoteOption voteOption = selectList.get(i);
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("vo_id", voteOption.getVoId());
            int delete = vom.delete(wrapper1);
            if (delete == 0) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean addOptions(Long vsId, int order, String[] optionsDate) {
        List<VoteOption> list = new ArrayList<>();
        for (int i = 0; i < optionsDate.length; i++) {
            String s = optionsDate[i];
            list.add(new VoteOption(vsId, s, order + i + 1));
        }
        int insertatch = vom.insertatch(list);
        if (insertatch != list.size()) {
            return false;
        }


        return true;
    }

    @Override
    public boolean updateOptions(String[] options, Long vsId) {
        for (int i = 0; i < options.length; i++) {
            String option = options[i];
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("vs_id", vsId);
            wrapper.eq("vo_order", i + 1);
            VoteOption voteOption = new VoteOption();
            voteOption.setVoOption(option);
            int update = vom.update(voteOption, wrapper);
            if (update == 0) {
                System.out.println("修改失败==============》" + update);
                return false;
            }
        }

        return true;
    }

    @Override
    @Transactional
    public int insertatch(List<VoteOption> list) {
        return vom.insertatch(list);
    }
}
